package com.sqss;

import com.sun.istack.internal.NotNull;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BaseConn {

    private static Connection connection;

    public static Connection getConnection() {
        return connection;
    }


    static {
        // 连接数据库 <@NOTES> 连接数据库, 失败,退出程序...</NOTES>
        try {
            Class.forName(com.mysql.jdbc.Driver.class.getName());
            // parse
            String url = "jdbc:mysql://localhost:3306/test?user=root&password=123456";
            connection = DriverManager.getConnection(url);
            if (connection == null) {
                System.out.println("连接数据库失败 ");
                System.exit(2);
            } else {
                System.out.println("连接数据库成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }

    }

    public static <T> List<T> query(@NotNull String sql, @NotNull List<Object> objects, Class clazz) {

        List<T> lists = new ArrayList<>();
        if (sql == null) {
            throw new NullPointerException("sql could not bee null ");

        }
        try {
            CallableStatement statement = connection.prepareCall(sql);
            // 填充?
            for (int i = 0; i < objects.size(); i++) {
                statement.setObject(i + 1, objects.get(i));
            }
            ResultSet rs = statement.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            while (rs.next()) {
                // 通过反射生成对象
                Object refo = clazz.newInstance();
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    Field field = clazz.getDeclaredField(metaData.getColumnName(i + 1));
                    field.setAccessible(true);
                    Object value = rs.getObject(field.getName());
                    field.set(refo, value);
                }
                lists.add((T) refo);
            }
            // 关闭结果集合
            rs.close();
            // 关闭 ****
            statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lists;
    }
    /**
     * @param sql
     * @param objects
     * @return true 表示 成功修改
     */
    public static int  update(@NotNull String sql, @NotNull List<Object> objects) {
        int ok = 0;
        try {
            CallableStatement statement = connection.prepareCall(sql);
            for (int i = 0; i < objects.size(); i++) {
                statement.setObject(i + 1, objects.get(i));
            }
             int i = statement.executeUpdate();
            ok = i ;
            statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ok;
    }
    /**
     * @param sql
     * @param objects
     * @return true 表示 成功修改
     */
    public static int insert(@NotNull String sql, @NotNull List<Object> objects) {
        int ok = 0;
        try {
            CallableStatement statement = connection.prepareCall(sql);
            for (int i = 0; i < objects.size(); i++) {
                statement.setObject(i + 1, objects.get(i));
            }
            long l = statement.executeLargeUpdate();
            ok = (int) (l);
            // 获取自增长的id的值
            ResultSet rs = statement.getGeneratedKeys();
            if (rs.next()) {
                ok = rs.getInt(1);
            }
            // 关闭结果集,查询
            rs.close();
            statement.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ok;
    }

    public static void main(String[] args) {

    }
}
